package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/1/7 13:50
 * @description：https://leetcode-cn.com/problems/string-to-integer-atoi/
 */
public class 字符串转换整数 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(solution.myAtoi("   +-21asd"));
    }

    class Solution {
        public int myAtoi(String s) {
            Automaton automaton = new Automaton();
            for (char c : s.toCharArray()) {
                automaton.get(c);
            }
            return (int) (automaton.sign * automaton.ans);
        }

    }

    class Automaton {
        // 1:+
        // -1:-
        private int sign = 1;
        private long ans = 0L;
        private String state = "start";
        private Map<String, String[]> tables = new HashMap<>();
        {
            tables.put("start", new String[]{"start", "signed", "in_number", "end"});
            tables.put("signed", new String[]{"end", "end", "in_number", "end"});
            tables.put("in_number", new String[]{"end", "end", "in_number", "end"});
            tables.put("end", new String[]{"end", "end", "end", "end"});
        }

        public void get(char c) {
            if (state.equals("end")) return;
            state = tables.get(state)[get_col(c)];
            if ("in_number".equals(state)) {
                ans = ans * 10 + c - '0';
                ans = sign == 1 ? Math.min(ans, Integer.MAX_VALUE)
                        : Math.min(ans, -(long)Integer.MIN_VALUE);
            } else if ("signed".equals(state)) {
                sign = c == '+' ? 1 : -1;
            }
        }

        private int get_col(char c) {
            if (c == ' ') return 0;
            if (c == '+' || c == '-') return 1;
            if (Character.isDigit(c)) return 2;
            return 3;
        }
    }

    /*
    class Solution {
        public int myAtoi(String s) {
            s = s.trim();
            StringBuilder ans = new StringBuilder();
            if (s.length() == 0) return 0;
            int index = 0;
            if (s.charAt(index) == '+' || s.charAt(index) == '-') {
                ans.append(s.charAt(index++));
            }
            if (ans.length() == 0) {
                ans.append("+");
            }
            for (; index < s.length(); index++) {
                char c = s.charAt(index);
                if (c == '0' && ans.length() == 0) continue;

                if (Character.isDigit(c)) {
                    ans.append(c);
                    if (ans.length() > 10) {
                        long l = Long.parseLong(ans.toString());
                        if (l < Integer.MIN_VALUE) return Integer.MIN_VALUE;
                        if (l > Integer.MAX_VALUE) return Integer.MAX_VALUE;
                    }
                } else {
                    break;
                }
            }
            return ans.length() == 1 ? 0 : Integer.parseInt(ans.toString());
        }
    }
     */
}